package com.easemob.helpdesk.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.easemob.helpdesk.widget.photoview.PhotoView;

import java.io.ByteArrayOutputStream;

/**
 * Created by xiaolong.wei on 2019/3/25.
 */

public class PreviewPicturePagerAdapter extends PagerAdapter {

    private String[] mFileUrl;
    private Context mContext;
    private byte[] bytes;

    public PreviewPicturePagerAdapter(String[] mFileUrl, Context context) {
        this.mFileUrl = mFileUrl;
        this.mContext = context;
    }

    public PreviewPicturePagerAdapter(Context mContext, byte[] bytes) {
        this.mContext = mContext;
        this.bytes = bytes;
    }

    @Override
    public int getCount() {
        return mFileUrl != null ? mFileUrl.length : (bytes != null ? 1 : 0);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        ImageView imageView = new ImageView(mContext);
        PhotoView imageView = new PhotoView(mContext);
        imageView.setZoomable(true);
        imageView.setMaxScale(3.0f);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(params);
        if (mFileUrl != null) {
            Glide.with(mContext).load(mFileUrl[position]).dontAnimate().into(imageView);
        }else if (bytes!= null){

            Glide.with(mContext).load(bytes).into(imageView);
        }
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(((View) object));
    }
}
